[!if FPCPRT != ""]
	Copyright (c) 2011-2012 by MicroStrategy, Inc.  This copyrighted material
	is the confidential, unpublished property of MicroStrategy.  This
	copyright notice and any other copyright notices included in machine
	readable copies must be reproduced on all authorized copies.
[!endif]
//=======================================================================
[!output COPYRIGHT_MSG]
//
//		File:	FPFunctions.cpp
//
//		Date:	[!output FPDATE]
//
//		Desc:	Implementation of user's function plug-ins.
//=======================================================================

//=======================================================================
// includes
//=======================================================================
#include "StdAfx.h"
#include "FPClasses.h"
[!if FUNCSIM_COUNT]

//=======================================================================
// [!output FUNCSIM_CLASSNAME]
//=======================================================================
[!loop = FUNCSIM_COUNT]

//-----------------------------------------------------------------------
//	[!output FUNCSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCSIM_FUNCNAME_[!output LOOP_COUNTER]]()
//
//	DESCRIPTION:
//		[!output OUTPUT_DIRECTIVE] FUNCSIM_FUNCDESC_[!output LOOP_COUNTER]]
//
//	PARAMETERS:
//		nParam		number of elements in pParam
//		pParam		pointer to one-dimensional array of input from engine (see FP Inputs)
//		pResult		pointer to scalar result (see FP Output)
//		pFlag		pointer to result flag
//
//		FP Inputs:[!output OUTPUT_DIRECTIVE] FUNCSIM_INPARAM_COMMENTS_[!output LOOP_COUNTER]]
//		FP Output:[!output OUTPUT_DIRECTIVE] FUNCSIM_OUTPARAM_COMMENT_[!output LOOP_COUNTER]]
//
[!output IF_DIRECTIVE] FUNCSIM_PROP_COMMENTS_[!output LOOP_COUNTER] != ""]
//	PROPERTIES:[!output OUTPUT_DIRECTIVE] FUNCSIM_PROP_COMMENTS_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
//	PROPERTIES:
//		None
[!output ENDIF_DIRECTIVE]
//
//	RETURNS:
//		S_OK
//		E_FAIL
//-----------------------------------------------------------------------
STDMETHODIMP [!output FUNCSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCSIM_FUNCNAME_[!output LOOP_COUNTER]](
	Int_32 nParam,
	double *pParam,
	double *pResult,
	DSSData_Flags *pFlag)
{
[!output IF_DIRECTIVE] FUNCSIM_RFUNC_[!output LOOP_COUNTER] != ""]
	Int_32 lRSize;
[!output IF_DIRECTIVE] FUNCSIM_REPCNT_[!output LOOP_COUNTER] != "0"]
	const Int_32 INCNT = [!output OUTPUT_DIRECTIVE] FUNCSIM_INCNT_[!output LOOP_COUNTER]];
	const Int_32 REPCNT = [!output OUTPUT_DIRECTIVE] FUNCSIM_REPCNT_[!output LOOP_COUNTER]];
[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
	const wchar_t FUNC_NAME[] = L"[!output FUNCSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCSIM_FUNCNAME_[!output LOOP_COUNTER]]";

	try
	{
[!output IF_DIRECTIVE] FUNCSIM_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// pass input values to R
[!output OUTPUT_DIRECTIVE] FUNCSIM_SETRVARS_[!output LOOP_COUNTER]]
		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch result from R
[!output OUTPUT_DIRECTIVE] FUNCSIM_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
		// initialize
		*pFlag = DssDataInvalid;

		// TODO: add your code here

		//*pResult = your result;
		*pFlag = DssDataOk;

[!output ENDIF_DIRECTIVE]
		return S_OK;
	}
	catch(...)
	{
		return E_FAIL;
	}
}
[!endloop]
[!endif]
[!if FUNCNNSIM_COUNT]

//=======================================================================
// [!output FUNCNNSIM_CLASSNAME]
//=======================================================================
[!loop = FUNCNNSIM_COUNT]

//-----------------------------------------------------------------------
//	[!output FUNCNNSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNSIM_FUNCNAME_[!output LOOP_COUNTER]]()
//
//	DESCRIPTION:
//		[!output OUTPUT_DIRECTIVE] FUNCNNSIM_FUNCDESC_[!output LOOP_COUNTER]]
//
//	PARAMETERS:
//		nParam		number of elements in pParam
//		pParam		pointer to one-dimensional array of input from engine (see FP Inputs)
//		pResult		pointer to scalar result (see FP Output)
//		pFlag		pointer to result flag
//
//		FP Inputs:[!output OUTPUT_DIRECTIVE] FUNCNNSIM_INPARAM_COMMENTS_[!output LOOP_COUNTER]]
//		FP Output:[!output OUTPUT_DIRECTIVE] FUNCNNSIM_OUTPARAM_COMMENT_[!output LOOP_COUNTER]]
//
[!output IF_DIRECTIVE] FUNCNNSIM_PROP_COMMENTS_[!output LOOP_COUNTER] != ""]
//	PROPERTIES:[!output OUTPUT_DIRECTIVE] FUNCNNSIM_PROP_COMMENTS_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
//	PROPERTIES:
//		None.
[!output ENDIF_DIRECTIVE]
//
//	RETURNS:
//		S_OK
//		E_FAIL
//-----------------------------------------------------------------------
STDMETHODIMP [!output FUNCNNSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNSIM_FUNCNAME_[!output LOOP_COUNTER]](
	Int_32 nParam,
	VARIANT *pParam,
	VARIANT *pResult,
	DSSData_Flags *pFlag)
{
[!output IF_DIRECTIVE] FUNCNNSIM_ALLOCLOCALMEM_[!output LOOP_COUNTER] != ""]
	const Int_32 INCNT = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_INCNT_[!output LOOP_COUNTER]];  // number of inputs
	const Int_32 REPCNT = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_REPCNT_[!output LOOP_COUNTER]];  // number of repeated inputs
	Int_32 i, j;
	Int_32 lRepCnt;  // number of times inputs are repeated
[!output ENDIF_DIRECTIVE]
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_INPARAMDECLS_[!output LOOP_COUNTER]]	HRESULT hr = S_OK;
[!output IF_DIRECTIVE] FUNCNNSIM_RFUNC_[!output LOOP_COUNTER] != ""]
	VARIANT varElem;
	Int_32 lIndex		= 0;
	Int_32 lRSize;
[!output IF_DIRECTIVE] FUNCNNSIM_REPCNT_[!output LOOP_COUNTER] != "0"]
	const Int_32 INCNT = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_INCNT_[!output LOOP_COUNTER]];
	const Int_32 REPCNT = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_REPCNT_[!output LOOP_COUNTER]];
[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
	const wchar_t FUNC_NAME[] = L"[!output FUNCNNSIM_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNSIM_FUNCNAME_[!output LOOP_COUNTER]]";

	try
	{
[!output IF_DIRECTIVE] FUNCNNSIM_ALLOCLOCALMEM_[!output LOOP_COUNTER] != ""]
		// allocate local memory
		lRepCnt = (nParam-INCNT)/REPCNT + 1 + (nParam-INCNT) % REPCNT;
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_ALLOCLOCALMEM_[!output LOOP_COUNTER]]
[!output ENDIF_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCNNSIM_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// initialize
		VariantInit(&varElem);

		// pass input values to R
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_SETRVARS_[!output LOOP_COUNTER]]
		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch result from R
		pResult->vt = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_OUTDATAVT_[!output LOOP_COUNTER]];
[!output IF_DIRECTIVE] FUNCNNSIM_OUTDATAVT_[!output LOOP_COUNTER] == "VT_BSTR"]
		pResult->bstrVal = NULL;
[!output ENDIF_DIRECTIVE]
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
		// get data values from variants
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_GETPARAMFROMVARIANT_[!output LOOP_COUNTER]]
		// initialize
		*pFlag = DssDataInvalid;

		// TODO: add your code here

		//pResult->vt = [!output OUTPUT_DIRECTIVE] FUNCNNSIM_OUTDATAVT_[!output LOOP_COUNTER]];
		//pResult->[!output OUTPUT_DIRECTIVE] FUNCNNSIM_OUTDATAVAL_[!output LOOP_COUNTER]] = your result;

		*pFlag = DssDataOk;
[!output ENDIF_DIRECTIVE]
	}
	catch(HRESULT catchHR)
	{
		hr = catchHR;
	}
	catch(...)
	{
		hr = E_FAIL;
	}

[!output IF_DIRECTIVE] FUNCNNSIM_FREELOCALMEM_[!output LOOP_COUNTER] != ""]
	// release local memory
[!output OUTPUT_DIRECTIVE] FUNCNNSIM_FREELOCALMEM_[!output LOOP_COUNTER]]
[!output ENDIF_DIRECTIVE]
	return hr;
}
[!endloop]
[!endif]
[!if FUNCGEN_COUNT]

//=======================================================================
// [!output FUNCGEN_CLASSNAME]
//=======================================================================
[!loop = FUNCGEN_COUNT]

//-----------------------------------------------------------------------
//	[!output FUNCGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCGEN_FUNCNAME_[!output LOOP_COUNTER]]()
//
//	DESCRIPTION:
//		[!output OUTPUT_DIRECTIVE] FUNCGEN_FUNCDESC_[!output LOOP_COUNTER]]
//
//	PARAMETERS:
//		nSize		number of elements in first dimension of m[!output OUTPUT_DIRECTIVE] FUNCGEN_INDEX_[!output LOOP_COUNTER]]_pData
[!output IF_DIRECTIVE] FUNCGEN_OUTPARAMVEC_[!output LOOP_COUNTER] != ""]
//		nDataSize	number of elements returned in pResult
//		pResult		pointer to one-dimensional array of result (see FP Output)
[!output ELSE_DIRECTIVE]
//		nDataSize	output is scalar; must set to 1
//		pResult		pointer to scalar output (see Output)
[!output ENDIF_DIRECTIVE]
//		pFlag		pointer to one-dimensional array of result flags
//
//		FP Inputs:[!output OUTPUT_DIRECTIVE] FUNCGEN_INPARAM_COMMENTS_[!output LOOP_COUNTER]]
//		FP Output:[!output OUTPUT_DIRECTIVE] FUNCGEN_OUTPARAM_COMMENT_[!output LOOP_COUNTER]]
//
[!output IF_DIRECTIVE] FUNCGEN_PROP_COMMENTS_[!output LOOP_COUNTER] != ""]
//	PROPERTIES:[!output OUTPUT_DIRECTIVE] FUNCGEN_PROP_COMMENTS_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
//	PROPERTIES:
//		None.
[!output ENDIF_DIRECTIVE]
//
//	RETURNS:
//		S_OK
//		E_FAIL
//-----------------------------------------------------------------------
STDMETHODIMP [!output FUNCGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCGEN_FUNCNAME_[!output LOOP_COUNTER]](
	Int_32 nSize,
	Int_32 *nDataSize,
	double *pResult,
	DSSData_Flags *pFlag)
{
[!output IF_DIRECTIVE] FUNCGEN_RFUNC_[!output LOOP_COUNTER] != ""]
	Int_32 lRSize;
[!output ELSE_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
	Int_32 i;

[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
	const wchar_t FUNC_NAME[] = L"[!output FUNCGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCGEN_FUNCNAME_[!output LOOP_COUNTER]]";
[!output IF_DIRECTIVE] FUNCGEN_OUTPARAMVEC_[!output LOOP_COUNTER] != ""]

	try
	{
[!output IF_DIRECTIVE] FUNCGEN_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch results from R
[!output OUTPUT_DIRECTIVE] FUNCGEN_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
		// perform first-pass calculations on input
		for(i=0;i<m_lInputSize;i++)
		{
			// TODO: add your code here
		}
[!output ELSE_DIRECTIVE]
		// TODO: add your code here

[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCGEN_RFUNC_[!output LOOP_COUNTER] == ""]
		// result should be a one-dimensional array of doubles[nSize]
		for(i=0;i<nSize;i++)
		{
			pFlag[i] = DssDataInvalid;

			// TODO: add your code here

			//pResult[i] = your result;
			pFlag[i] = DssDataOk;
		}

[!output ENDIF_DIRECTIVE]
		// set number of elements returned (must be <= nSize)
		*nDataSize = nSize;
	}
	catch(...)
	{
		return E_FAIL;
	}
[!output ELSE_DIRECTIVE]
	try
	{
		// result is a scalar
		*pFlag = DssDataInvalid;

[!output IF_DIRECTIVE] FUNCGEN_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch result from R
[!output OUTPUT_DIRECTIVE] FUNCGEN_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
		// perform first-pass calculations on input
		for(i=0;i<m_lInputSize;i++)
		{
			// TODO: add your code here
		}
[!output ELSE_DIRECTIVE]
		// TODO: add your code here

[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCGEN_RFUNC_[!output LOOP_COUNTER] == ""]
		//*pResult = your result;
		*pFlag = DssDataOk;
[!output ENDIF_DIRECTIVE]
		*nDataSize = 1;  // output is scalar...must set to 1
	}
	catch(...)
	{
		return E_FAIL;
	}
[!output ENDIF_DIRECTIVE]

	return S_OK;
}
[!endloop]
[!endif]
[!if FUNCNNGEN_COUNT]

//=======================================================================
// [!output FUNCNNGEN_CLASSNAME]
//=======================================================================
[!loop = FUNCNNGEN_COUNT]

//-----------------------------------------------------------------------
//	[!output FUNCNNGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNGEN_FUNCNAME_[!output LOOP_COUNTER]]()
//
//	DESCRIPTION:
//		[!output OUTPUT_DIRECTIVE] FUNCNNGEN_FUNCDESC_[!output LOOP_COUNTER]]
//
//	PARAMETERS:
//		nSize		number of elements in each "m[!output OUTPUT_DIRECTIVE] FUNCNNGEN_INDEX_[!output LOOP_COUNTER]]_" member variable
[!output IF_DIRECTIVE] FUNCNNGEN_OUTPARAMVEC_[!output LOOP_COUNTER] != ""]
//		nDataSize	number of elements returned in pResult
//		pResult		pointer to one-dimensional array of result (see FP Output)
[!output ELSE_DIRECTIVE]
//		nDataSize	output is scalar; must set to 1
//		pResult		pointer to scalar output (see Output)
[!output ENDIF_DIRECTIVE]
//		pFlag		pointer to one-dimensional array of result flags
//
//		FP Inputs:[!output OUTPUT_DIRECTIVE] FUNCNNGEN_INPARAM_COMMENTS_[!output LOOP_COUNTER]]
//		FP Output:[!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTPARAM_COMMENT_[!output LOOP_COUNTER]]
//
[!output IF_DIRECTIVE] FUNCNNGEN_PROP_COMMENTS_[!output LOOP_COUNTER] != ""]
//	PROPERTIES:[!output OUTPUT_DIRECTIVE] FUNCNNGEN_PROP_COMMENTS_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
//	PROPERTIES:
//		None.
[!output ENDIF_DIRECTIVE]
//
//	RETURNS:
//		S_OK
//		E_FAIL
//-----------------------------------------------------------------------
STDMETHODIMP [!output FUNCNNGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNGEN_FUNCNAME_[!output LOOP_COUNTER]](
	Int_32 nSize,
	Int_32 *nDataSize,
	VARIANT *pResult,
	DSSData_Flags *pFlag)
{
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] != ""]
	Int_32 lRSize;
	HRESULT hr = S_OK;
[!output ELSE_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCNNGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
	Int_32 i;
[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
	const wchar_t FUNC_NAME[] = L"[!output FUNCNNGEN_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCNNGEN_FUNCNAME_[!output LOOP_COUNTER]]";
[!output IF_DIRECTIVE] FUNCNNGEN_OUTPARAMVEC_[!output LOOP_COUNTER] != ""]
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] == ""]
	VARIANT output;
	HRESULT hr = S_OK;
[!output ENDIF_DIRECTIVE]

	try
	{
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// result should be a one-dimensional array of Variants[nSize]
		pResult->vt = VT_ARRAY|VT_VARIANT;
		SAFEARRAYBOUND lSABound;
		lSABound.lLbound = 0;
		lSABound.cElements = nSize;
		pResult->parray = SafeArrayCreate(VT_VARIANT, 1, &lSABound);

		// fetch results from R
[!output OUTPUT_DIRECTIVE] FUNCNNGEN_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCNNGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
		// perform first-pass calculations on input
		for(i=0;i<m_lInputSize;i++)
		{
			// TODO: add your code here
		}
[!output ELSE_DIRECTIVE]
		// TODO: add your code here

[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] == ""]
		// result should be a one-dimensional array of Variants[nSize]
		pResult->vt = VT_ARRAY|VT_VARIANT;
		SAFEARRAYBOUND lSABound;
		lSABound.lLbound = 0;
		lSABound.cElements = nSize;
		pResult->parray = SafeArrayCreate(VT_VARIANT, 1, &lSABound);

		for(i=0;i<nSize;i++)
		{
			pFlag[i] = DssDataInvalid;
			VariantInit(&output);
			//output.vt = [!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTDATAVT_[!output LOOP_COUNTER]];
			// TODO: add your code here

			//output.[!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTDATAVAL_[!output LOOP_COUNTER]] = your result;
			hr=SafeArrayPutElement(pResult->parray, (LONG *)&i, &output);
			VariantClear(&output);
			pFlag[i] = DssDataOk;
		}

[!output ENDIF_DIRECTIVE]
		// set number of elements returned (must be <= nSize)
		*nDataSize = nSize;
	}
	catch(...)
	{
		return E_FAIL;
	}
[!output ELSE_DIRECTIVE]

	try
	{
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch result from R
		pResult->vt = [!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTDATAVT_[!output LOOP_COUNTER]];
[!output IF_DIRECTIVE] FUNCNNGEN_OUTDATAVT_[!output LOOP_COUNTER] == "VT_BSTR"]
		pResult->bstrVal = NULL;
[!output ENDIF_DIRECTIVE]
[!output OUTPUT_DIRECTIVE] FUNCNNGEN_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
		// result is a scalar
		*pFlag = DssDataInvalid;

[!output IF_DIRECTIVE] FUNCNNGEN_VECTOR_INPUT_[!output LOOP_COUNTER] != ""]
		// perform first-pass calculations on input
		for(i=0;i<m_lInputSize;i++)
		{
			// TODO: add your code here
		}
[!output ELSE_DIRECTIVE]
		// TODO: add your code here

[!output ENDIF_DIRECTIVE]
[!output ENDIF_DIRECTIVE]
[!output IF_DIRECTIVE] FUNCNNGEN_RFUNC_[!output LOOP_COUNTER] == ""]
		pResult->vt = [!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTDATAVT_[!output LOOP_COUNTER]];
		//pResult->[!output OUTPUT_DIRECTIVE] FUNCNNGEN_OUTDATAVAL_[!output LOOP_COUNTER]] = your result;
		*pFlag = DssDataOk;
[!output ENDIF_DIRECTIVE]
		*nDataSize = 1;  // output is scalar...must set to 1
	}
	catch(...)
	{
		return E_FAIL;
	}
[!output ENDIF_DIRECTIVE]

	return S_OK;
}
[!endloop]
[!endif]
[!if FUNCAGG_COUNT]

//=======================================================================
// [!output FUNCAGG_CLASSNAME]
//=======================================================================
[!loop = FUNCAGG_COUNT]

//-----------------------------------------------------------------------
//	[!output FUNCAGG_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCAGG_FUNCNAME_[!output LOOP_COUNTER]]()
//
//	DESCRIPTION:
//		[!output OUTPUT_DIRECTIVE] FUNCAGG_FUNCDESC_[!output LOOP_COUNTER]]
//
//	PARAMETERS:
//		nSize		number of elements in pParam
//		pParam		pointer to one-dimensional array of input from engine (see FP Input)
//		pResult		pointer to scalar result (see FP Output)
//		pFlag		pointer to result flag
//
//		FP Input:[!output OUTPUT_DIRECTIVE] FUNCAGG_INPARAM_COMMENTS_[!output LOOP_COUNTER]]
//		FP Output:[!output OUTPUT_DIRECTIVE] FUNCAGG_OUTPARAM_COMMENT_[!output LOOP_COUNTER]]
//
[!output IF_DIRECTIVE] FUNCAGG_PROP_COMMENTS_[!output LOOP_COUNTER] != ""]
//	PROPERTIES:[!output OUTPUT_DIRECTIVE] FUNCAGG_PROP_COMMENTS_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
//	PROPERTIES:
//		None.
[!output ENDIF_DIRECTIVE]
//
//	RETURNS:
//		S_OK
//		E_FAIL
//-----------------------------------------------------------------------
STDMETHODIMP [!output FUNCAGG_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCAGG_FUNCNAME_[!output LOOP_COUNTER]](
	Int_32 nSize,
	double *pParam,
	double *pResult,
	DSSData_Flags *pFlag)
{
[!output IF_DIRECTIVE] FUNCAGG_RFUNC_[!output LOOP_COUNTER] != ""]
	Int_32 lRSize;
[!output ENDIF_DIRECTIVE]
	const wchar_t FUNC_NAME[] = L"[!output FUNCAGG_CLASSNAME]::[!output OUTPUT_DIRECTIVE] FUNCAGG_FUNCNAME_[!output LOOP_COUNTER]]";

	try
	{
[!output IF_DIRECTIVE] FUNCAGG_RFUNC_[!output LOOP_COUNTER] != ""]
		// check R initialization
		if(!m_pRSupp)
		{
			// problems connecting to R
			INIT_ERROR_MSG(L"R initialization failed.")
			return E_FAIL;
		}

		// pass input values to R
[!output OUTPUT_DIRECTIVE] FUNCAGG_SETRVARS_[!output LOOP_COUNTER]]
		// submit R script
		if(m_pRSupp->SubmitR(m[!output LOOP_COUNTER]_RScriptFile))
		{
			// failure while processing R
			INIT_ERROR_MSG(L"R script execution failed.")
			return E_FAIL;
		}

		// fetch result from R
[!output OUTPUT_DIRECTIVE] FUNCAGG_GETRVAR_[!output LOOP_COUNTER]]
[!output ELSE_DIRECTIVE]
		// initialize
		*pFlag = DssDataInvalid;

		// TODO: add your code here

		*pFlag = DssDataOk;

[!output ENDIF_DIRECTIVE]
		return S_OK;
	}
	catch(...)
	{
		return E_FAIL;
	}
}
[!endloop]
[!endif]
